package com.team.easybuy.controller;

import com.alibaba.fastjson.JSON;
import com.team.easybuy.entity.Car;
import com.team.easybuy.service.CarService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/car")
public class CarController {

    @Resource
    private CarService carService;

    @RequestMapping("/addProInCar")
    public String addProInCar(@RequestBody Map<String, Object> params) {
        String str = JSON.toJSONString(params.get("car"));
        Car car = JSON.parseObject(str, Car.class);
        int id = Integer.parseInt(params.get("id").toString());
        return carService.addProInCar(car, id);
    }

    @RequestMapping("/loadCarList/{id}")
    public List<Car> loadCarList(@PathVariable String id) {
        return carService.loadCarList(Integer.valueOf(id));
    }

    @RequestMapping("/addNum")
    public String addNum(@RequestParam("pId") Integer pId,
                         @RequestParam("id") Integer id,
                         @RequestParam("stock") Integer stock) {
        return carService.addNumByPro(pId, id, stock);
    }

    @RequestMapping("/delNum")
    public String delNum(@RequestParam("pId") Integer pId,
                         @RequestParam("id") Integer id) {
        return carService.delNumByPro(pId, id);
    }

    @RequestMapping("/delInCar")
    public boolean delInCar(@RequestParam("pId") Integer pId,
                            @RequestParam("id") Integer id) {
        return carService.delInCar(pId, id);
    }

    @RequestMapping("/delAllCar")
    public boolean delAllCar(@RequestParam("id") Integer id) {
        return carService.delAllCar(id);
    }
}
